using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class NoticePanelData
{
    public string title;
    public string content;
    public string secondContent;
    public string btnLeftTxt;
    public string btnRightTxt;
    public Action onConfirm;
    public Action onCancel;
    public bool selfClose = true;
    public bool onlyMsg = false;

    void Init(string _title, string _content,string _secondContent, string _btnConfirmText, string _btnCancelText, Action _onConfirm, Action _onCancel,bool _selfClose,bool onlyMsg)
    {
        this.title = _title;
        this.content = _content;
        this.btnLeftTxt = _btnCancelText;
        this.btnRightTxt = _btnConfirmText;
        this.secondContent = _secondContent;
        this.selfClose = _selfClose;
        onConfirm = _onConfirm;
        this.onlyMsg = onlyMsg;
        onCancel = _onCancel;
    }
    public NoticePanelData(string _title, string _content, string _secondContent, string _btnConfirmText, string _btnCancelText, Action _onConfirm, Action _onCancel)
    {
        Init(_title, _content, _secondContent, _btnConfirmText, _btnCancelText, _onConfirm, _onCancel,true,false);
    }
    public NoticePanelData(string _title,string _content, string _btnConfirmText, string _btnCancelText,Action _onConfirm, Action _onCancel)
    {
        Init(_title, _content,"", _btnConfirmText, _btnCancelText, _onConfirm, _onCancel, true,false);
    }
    public NoticePanelData(string _title, string _content, string _btnConfirmText, string _btnCancelText, Action _onConfirm, Action _onCancel, bool _selfClose)
    {
        Init(_title, _content, "", _btnConfirmText, _btnCancelText, _onConfirm, _onCancel, _selfClose,false);
    }
    public NoticePanelData(string _title, string _content, string _btnText, Action _onConfirm)
    {
        Init(_title, _content, "", _btnText,"", _onConfirm,null, true,false);
    }
    public NoticePanelData(string _content, Action _onConfirm)
    {
        Init(Localization.instance.GetValue("news"), _content, "", Localization.instance.GetValue("confirm"), "", _onConfirm, null, true,false);//消息//确认
    }
    public NoticePanelData( string _content)
    {
        Init(Localization.instance.GetValue("news"), _content, "", Localization.instance.GetValue("confirm"), "", null, null, true,false);//消息//确认
    } 
    
    public NoticePanelData( string _content,bool onlyMsg)
    {
        Init(Localization.instance.GetValue("news"), _content, "", Localization.instance.GetValue("confirm"), "", null, null, true,onlyMsg);//消息//确认
    }
}

public class NoticePanel : PanelCommon
{
    public Text titleText;
    public Text contentText;
  //  public Text secondContentText;
    public Text btnLeftText;
    public Text btnRightText;

    public UnityEngine.GameObject leftBtnGo;
    public UnityEngine.GameObject rightBtnGo;

    Action m_OnConfirm;
    Action m_OnCancel;
    bool m_SelfClose;

   // bool m_IsShowing;
   // public bool isShowing => m_IsShowing;

    private bool m_IsShowingNotice;
    public bool isShowingNotice => m_IsShowingNotice;

    public bool isShowingMsg
    {
        get { return m_IsShowingMsg; }
    }

    private bool m_IsShowingMsg;

    public AnimationCurve largenCurve;
    public AnimationCurve smallenCurve;
    
    [SerializeField]
    ZTransformAnim mZTransformAnim;

    [SerializeField]
    Transform m_WidgetsParent;

    bool m_StopFadeOut = false;

    public override void Awake()
    {
        base.Awake();
        if (mZTransformAnim == null) mZTransformAnim = GetComponent<ZTransformAnim>();
        if (mZTransformAnim == null) mZTransformAnim = gameObject.AddComponent<ZTransformAnim>();

        m_WidgetsParent.localScale = Vector3.zero;
        this.m_WidgetsParent.gameObject.SetActive(false);
        
        if (m_BlackBG)
            m_BlackBG.gameObject.SetActive(false);
        
        this.m_UICanvas.renderMode = RenderMode.ScreenSpaceOverlay;
        
    }

    // Use this for initialization
    public override void Start () {
        base.Start();
        
        //FadeInAnim();

        var rectTransform = GetComponent<RectTransform>();

        ZUIAnimTools.GetImgTxtAlpha(rectTransform, out m_ImgAlphas, out m_TextAlphas, out m_RawImgAlphas,out  m_TmptxtAlphas);
    }

    protected override void OnDestroy()
    {
        m_OnConfirm = null;
        m_OnCancel = null;
    }
    Action m_onShowEnd;
    public override void PlayShowAnim(Action onEnd = null)
    {
        //  m_IsShowing = true;
        m_onShowEnd = onEnd;

        PlayAnim(showAnimTime);
        
        m_StopFadeOut = true;
        
        AudioController.instance.PlayUISound(Audio.ui_scale);

    }

    void PlayAnim(float animTime)
    {
        MZuiAnimTools.StopAllAnim();
        mZTransformAnim.StopAllAnim();

        this.m_WidgetsParent.gameObject.SetActive(true);
        if (m_BlackBG)
            m_BlackBG.gameObject.SetActive(true);
        
        var rectTransform = GetComponent<RectTransform>();
        //m_UIAnim.SetImgTxtAlpha(rectTransform, 0);

        MZuiAnimTools.InitCurve(showAnimCurve);
        MZuiAnimTools.UIAlpha(rectTransform, m_ImgAlphas, m_TextAlphas, m_RawImgAlphas,m_TmptxtAlphas ,animTime, OnShowAnimEnd, ZUIAnimTools.EaseType.Curve);

        mZTransformAnim.SetCurve(this.largenCurve);
        mZTransformAnim.ScaleTo(m_WidgetsParent, Vector3.one, animTime, null);
    }
    
    protected override void OnShowAnimEnd()
    {
        base.OnShowAnimEnd();
        m_onShowEnd?.Invoke();
    }

    public override void PlayHideAnim(Action onEnd = null)
    {
        base.PlayHideAnim(onEnd);
        GameDebug.LogShowy("notice hide");

        m_StopFadeOut = false;

        //m_IsShowing = false;
        m_IsShowingMsg = false;
        m_IsShowingNotice = false;
        
        mZTransformAnim.SetCurve(this.smallenCurve);
        mZTransformAnim.ScaleTo(this.m_WidgetsParent, Vector3.zero, this.hideAnimTime, OnHideAnimEnd);
        
        DoActionDelay.DelayAction(0.18f, () =>
        {
            AudioController.instance.PlayUISound(Audio.ui_scale_disappear);
        });
    }

    protected override void OnHideAnimEnd()
    {
        base.OnHideAnimEnd();

        if (m_StopFadeOut)
        {
            return;
        }

        this.m_WidgetsParent.gameObject.SetActive(false);

        if (m_BlackBG)
            m_BlackBG.gameObject.SetActive(false);
    }

    public void Init(NoticePanelData data)
    {
        m_SelfClose = data.selfClose;
        m_OnConfirm = data.onConfirm;
        m_OnCancel = data.onCancel;
        
        titleText.text = data.title;
        contentText.text = data.content;
        btnRightText.text = data.btnRightTxt;

        /*if (string.IsNullOrEmpty(data.secondContent))
        {
            secondContentText.transform.parent.gameObject.SetActive(false);
        }
        else
        {
            secondContentText.text = data.secondContent;
        }*/

        if (string.IsNullOrEmpty(data.btnLeftTxt))
        {
            leftBtnGo.SetActive(false);
        }
        else
        {
            btnLeftText.text = data.btnLeftTxt;
            leftBtnGo.SetActive(true);
        }

        if (data.onlyMsg)
        {
            leftBtnGo.SetActive(false);
            rightBtnGo.SetActive(false);
            titleText.transform.parent.gameObject.SetActive(false);

            if (!m_IsShowingMsg)
            {  
                MZuiAnimTools.StopAllAnim();
                mZTransformAnim.StopAllAnim();

                this.m_WidgetsParent.gameObject.SetActive(true);
                if (m_BlackBG)
                    m_BlackBG.gameObject.SetActive(false);
        
                var rectTransform = GetComponent<RectTransform>();
                MZuiAnimTools.UIAlpha(rectTransform, m_ImgAlphas, m_TextAlphas, m_RawImgAlphas, m_TmptxtAlphas,0.02f, OnShowAnimEnd, ZUIAnimTools.EaseType.Curve);

                m_WidgetsParent.localScale = Vector3.one;

                m_IsShowingMsg = true;
            }
        }
        else
        {
            rightBtnGo.SetActive(true);

            titleText.transform.parent.gameObject.SetActive(true);
            m_IsShowingNotice = true;
            this.PlayShowAnim();
        }
    }

    public void AddOnClickCallBack(Action onConfirm,Action onCancel)
    {
        m_OnConfirm += onConfirm;
        m_OnCancel += onCancel;
    }

    public void OnClickConfirm()
    {
        if(this.m_SelfClose)
            PlayHideAnim();

        if (m_OnConfirm != null)
            m_OnConfirm();

    }

    public void OnClickCancel()
    {
        if (this.m_SelfClose)
            this.PlayHideAnim();

        if (m_OnCancel != null)
            m_OnCancel();
    }

    public void HideMsg()
    {
        if (m_IsShowingMsg)
        {
            Close();
        }
    }

    public void Close()
    {
        this.PlayHideAnim();
        
        if (m_OnCancel != null)
            m_OnCancel();
    }

    protected override void GoBackToView()
    {

    }

    
    //public new void GoBack()
    //{
    //    m_Controller.parentNav.GoBackToPreDC();
    //}

    // Update is called once per frame
    void Update () {
		
	}
}
